package cn.cs.controller;

import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.common.IdWorker;
import cn.cs.pojo.CommonFailures;
import cn.cs.service.CommonFailuresService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/commonFailures")
public class CommonFailuresController {
    @Resource
    private CommonFailuresService commonFailuresService;



    /**
     * 分页查询 常见故障
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getCommonFailuresList")
    public Dto getCommonFailuresList(Integer pageNumber, Integer pageSize, String pid)throws Exception{
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("pid", pid);
        return DtoUtil.returnSuccess("获取成功", commonFailuresService.queryCommonFailuresPageByMap(param, pageNumber, pageSize));
    }

    /**
     * 新增/编辑  常见故障
     * @param commonFailures
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateCommonFailures")
    public Dto updateCommonFailures(@RequestBody CommonFailures commonFailures){
        try {
            String msg = "新增成功";

            if (EmptyUtils.isEmpty(commonFailures.getId())) {
                commonFailures.setId(IdWorker.getId());
                commonFailures.setModifyDate(new Date());
                commonFailuresService.addCommonFailures(commonFailures);
            } else {
                commonFailuresService.modifyCommonFailures(commonFailures);
                msg = "修改成功";
            }
            return DtoUtil.returnSuccess(msg, "");
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


    /**
     * 删除 常见故障
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delCommonFailures")
    public Dto delCommonFailures(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += commonFailuresService.deleteCommonFailuresById(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("删除失败，请重试", "001");
        }
    }



    /**
     * 根据产品分类查询常见故障
     * @param pid
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/p/getCommonFailuresByPid")
    public Dto getCommonFailuresByPid(String pid)throws Exception{
        Map<String, Object> param = new HashMap<>();
        param.put("pid", pid);
        return DtoUtil.returnSuccess("获取成功", commonFailuresService.getCommonFailuresListByMap(param));
    }


}
